<template>
  <div>
    <h3>基本信息</h3>
    <div class="table-body">
      <vxe-toolbar style="height: 100px;">
        <template #buttons>
          <el-row style="margin-bottom: 20px;">
            <el-col :span="10">
              <sportChangesportItem ref="sport2sportItem" search="search" :showSportItem="false" :showPlayer="true" :playerIsPId="true"
                :sportChangesportItemData="sportChangesportItemData" @selectDataChange='searchSelectDataChange'
                title="教练员" />
            </el-col>
            <el-col :span="4">
              <el-select v-model="searchForm.politicalId" style="margin-left: 20px;" size="small" collapse-tags filterable clearable placeholder="选择政治面貌">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_political_type" :key="index"></el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="searchForm.educationTypeId" style="margin-left: 20px;" size="small" collapse-tags filterable clearable placeholder="选择受教育程度">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_education_type" :key="index"></el-option>
              </el-select>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8" style="display: flex;">
              <el-button type="success" icon="el-icon-plus" size="medium" @click="insertEvent">新增</el-button>
              <el-button type="success" icon="el-icon-printer" size="medium" @click="exportData">导出</el-button>
              <el-button type="success" icon="el-icon-printer" size="medium" @click="printEvent">打印</el-button>
            </el-col>
            <el-col :span="7">
              <el-input v-model="searchForm.enrollId" size="small" placeholder="输入注册号" style="width: 60%;"></el-input>
            </el-col>
            <el-col :span="5" style="display: flex;">
              <el-button type="success" size="small" icon="el-icon-s-promotion" @click="searchSubmit" style="margin: 0 10px;">
                搜索</el-button>
              <vxe-button @click="clearTableFilter" status="warning" icon="el-icon-refresh-right"> 清空</vxe-button>
            </el-col>
            <el-col :span="4">
              <vxe-input style="float: right;" v-model.trim.lazy="filterName" type="search" placeholder="快捷检索"
                @input="searchEvent"></vxe-input>
            </el-col>
          </el-row>
        </template>
      </vxe-toolbar>
      <vxe-table resizable ref="XTable" show-overflow border max-height="800" stripe highlight-hover-row highlight-current-row
        :loading="loading" :data="dataList" @sort-change="sortChange">
        <vxe-table-column field="player" title="运动员" sortable></vxe-table-column>
        <vxe-table-column field="sport" title="运动项目" sortable></vxe-table-column>
        <vxe-table-column field="coach" title="教练员" sortable></vxe-table-column>
        <vxe-table-column field="address" title="现在住址" width="150" sortable></vxe-table-column>
        <vxe-table-column field="political" title="政治面貌" sortable></vxe-table-column>
        <vxe-table-column field="educationType" title="教育程度" sortable></vxe-table-column>
        <vxe-table-column field="enrollId" title="注册号" sortable></vxe-table-column>
        <vxe-table-column field="enrollDivIPR" title="注册地（省/市）" sortable></vxe-table-column>
        <vxe-table-column field="enrollDivCT" title="注册地（市）" sortable></vxe-table-column>
        <vxe-table-column field="enrollDiv" title="注册地 区/县）" sortable></vxe-table-column>
        <vxe-table-column field="nativePlacePR" title="籍贯（省/市）" sortable></vxe-table-column>
        <vxe-table-column field="nativePlaceCT" title="籍贯（市）" sortable></vxe-table-column>
        <vxe-table-column field="nativePlace" title="籍贯（区/县）" sortable></vxe-table-column>
        <vxe-table-column field="phone" title="联系电话" width="110" sortable></vxe-table-column>
        <vxe-table-column field="email" title="邮箱" width="150" sortable></vxe-table-column>
        <vxe-table-column field="" title="操作" width="60">
          <template #default="{ row,rowIndex }">
            <el-button @click="editEvent(row,rowIndex)" type="success" size="medium" icon="el-icon-edit" circle></el-button>
            <el-button v-if="false" @click="deleteEvent(row)" type="danger" size="medium" icon="el-icon-delete" circle></el-button>
          </template>
        </vxe-table-column>
        <vxe-table-column field="createUser" title="录入人" sortable></vxe-table-column>
        <vxe-table-column field="checkUser" title="审核人" sortable>
          <template #default="{ row }">
            <span v-if="row.checkUserId!=0">{{row.checkUser}}</span>
            <el-link v-else @click="checkedEvent(row)" type="success">{{row.checkUser}}</el-link>
          </template>
        </vxe-table-column>
      </vxe-table>
      <vxe-pager :loading="loading" :current-page="page.currentPage" :page-size="page.pageSize" :total="page.totalResult"
        :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']" @page-change="handlePageChange">
      </vxe-pager>
    </div>
    <vxe-modal v-model="insertModal" title="基本信息">
      <template #default>
        <el-form inline-message ref="insertForm" :rules="rules" :model="insertForm" label-width="110px">
          <el-form-item label="运动员" prop="pId">
            <el-select v-model="insertForm.pId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_player" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="现在住址" prop="address">
            <el-input v-model="insertForm.address"></el-input>
          </el-form-item>
          <el-form-item label="政治面貌" prop="politicalId">
            <el-select v-model="insertForm.politicalId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_political_type" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="教育程度" prop="educationTypeId">
            <el-select v-model="insertForm.educationTypeId" filterable clearable style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_education_type" :key="index">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="注册号" prop="enrollId">
            <el-input v-model="insertForm.enrollId"></el-input>
          </el-form-item>
          <el-form-item label="注册地(省)">
            <el-select v-model="insertForm.enrollDivIPRId" filterable clearable  @change="enrollDivIPRIdChange" style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_divisionIdPR" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="注册地(市)">
            <el-select v-model="insertForm.enrollDivCTId" filterable clearable :disabled="enrollDivCTIdData.length==0"  @change="enrollDivCTIdChange" style="width: 100%;" placeholder="请选择">
              <el-option :label="item.name" :value="item.divId" v-for="(item,index) in enrollDivCTIdData" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="注册地(区/县)">
            <el-select v-model="insertForm.enrollDivId" filterable clearable :disabled="enrollDivIdData.length==0" style="width: 100%;" placeholder="请选择">
              <el-option :label="item.name" :value="item.divId" v-for="(item,index) in enrollDivIdData" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="籍贯(省)">
            <el-select v-model="insertForm.nativePlacePRId" filterable clearable  @change="nativePlacePRIdChange" style="width: 100%;" placeholder="请选择">
              <el-option :label="item" :value="key" v-for="(item,key,index) in dict_divisionIdPR" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="籍贯(市)">
            <el-select v-model="insertForm.nativePlaceCTId" filterable clearable :disabled="nativePlaceCTIdData.length==0"  @change="nativePlaceCTIdChange" style="width: 100%;" placeholder="请选择">
              <el-option :label="item.name" :value="item.divId" v-for="(item,index) in nativePlaceCTIdData" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="籍贯(区/县)">
            <el-select v-model="insertForm.nativePlaceId" filterable clearable :disabled="nativePlaceIdData.length==0" style="width: 100%;" placeholder="请选择">
              <el-option :label="item.name" :value="item.divId" v-for="(item,index) in nativePlaceIdData" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="insertForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="insertForm.email"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="warning" size="small" @click="insertModal = false">取消</el-button>
            <el-button type="success" size="small" icon="el-icon-s-promotion" @click="insertSave">保存</el-button>
          </el-form-item>
        </el-form>
      </template>
    </vxe-modal>
  </div>
</template>
<script>
  import {
    player_profile_playerByOrg,
    getPersonByOrgPlayer,
    player_profile_insert,
    player_profile_update,
    player_profile_check,
    player_profile_playerSelect,
    changeDivisionCY,changeDivision,
  } from '@/api/person.js'
  import {
    sport_by_org,
    pId2sportIds4emp1,
    userId2EmpName,pId2coachIds,availableCoach,pId2sportIds4player,
    political_type,
    selectDivisionPR,selectDivisionCY,selectDivisionCO
  } from '@/api/dict.js'
  import sportChangesportItem from '@/components/sport2sportItem/sport2sportItem.vue'
  export default {
    components: {
      sportChangesportItem
    },
    data() {
      return {
        defaultDataList: [],
        dataList: [],
        page: {
          currentPage: 1,
          pageSize: 10,
          totalResult: 0
        },
        loading: false,
        filterName: '',
        insertModal: false,
        dict_sport: {},
        dict_political_type: {},
        dict_pid_emp: {},
        dict_pid_birthday: {},
        dict_pid_gender: {},
        dict_pid_sport: {},
        dict_userId2empName: {},
        dict_divisionIdPR: {},
        dict_divisionIdCY: {},
        dict_division: {},
        enrollDivCTIdData: [],
        enrollDivIdData: [],
        nativePlaceCTIdData: [],
        nativePlaceIdData: [],
        dict_gender: {
          1: '男',
          2: '女'
        },
        dict_pid_idNo: {},
        dict_education_type: {
          1: "学龄前",
          2: "小学",
          3: "初中",
          4: "高中",
          5: "中专",
          6: "大专",
          7: "本科",
          8: "硕士",
          9: "博士",
          10: "博士后",
        },
        insertForm: {
          pId: '',
          address: '',
          politicalId: '',
          educationTypeId: '',
          enrollId: '',
          enrollDivIPRId: '',
          enrollDivCTId: '',
          enrollDivId: '',
          nativePlacePRId: '',
          nativePlaceCTId: '',
          nativePlaceId: '',
          phone: '',
          email: '',
        },
        searchForm: {
          coachId: '',
          pid: '',
          sportId: '',
          politicalId: '',
          educationTypeId: '',
          enrollId: '',
        },
        sportItemData: [],
        rules: {
          pId: [{
            required: true,
            message: '这是必填项',
            trigger: 'blur'
          }, ],
        },
        isInsert: true,
        flowId: '',
        pid: '',
        sportId: '',
        sportChangesportItemData: {},
        search: false,
        currentIndex: null,
      }
    },
    created() {
      this.init_dict().then(()=>{
        this.player_profile_playerByOrg()
      })
    },
    computed: {
      openDateValid() {
        if (this.insertForm.validDateBegin && this.insertForm.validDateEnd) {
          if (this.insertForm.validDateBegin > this.insertForm.validDateEnd) {
            this.$message.error('开始时间必须小于结束时间！')
            return false
          } else {
            return true
          }
        } else {
          return true
        }
      }
    },
    watch: {
      filterName(val) {
    	if(!val){
    	  this.page = {
    	  currentPage: 1,
    	  pageSize: 10,
    	  totalResult: 0,
    	  }
    	  this.search = false
    	  this.findList()
    	}
      }
    },
    methods: {
      searchSelectDataChange({
        sportId,
        person,
        playerId
      }) {
        this.searchForm.sportId = sportId
        this.searchForm.coachId = person
        this.searchForm.pid = playerId
      },
      clearTableFilter() {
        this.searchForm = {
          coachId: '',
          pid: '',
          sportId: '',
          politicalId: '',
          educationTypeId: '',
          enrollId: '',
        }
        this.sportChangesportItemData = {}
        this.player_profile_playerByOrg()
      },
      searchSubmit() {
        let data = {}
        if (this.searchForm.pid) data.pId = this.searchForm.pid
        if (this.searchForm.sportId) data.sportId = this.searchForm.sportId
        if (this.searchForm.coachId) data.coachId = this.searchForm.coachId
        if (this.searchForm.politicalId) data.politicalId = this.searchForm.politicalId
        if (this.searchForm.educationTypeId) data.educationTypeId = this.searchForm.educationTypeId
        if (this.searchForm.enrollId) data.enrollId = this.searchForm.enrollId
        player_profile_playerSelect(data).then(res => {
          if (res.code === 200 && res.data.length != 0) {
            this.page = {
              currentPage: 1,
              pageSize: 10,
              totalResult: 0
            }
            this.init_data(res.data)
          } else {
            this.defaultDataList = []
          }
        }).then(() => {
          this.findList()
        })
      },
      deleteEvent(row) {
        this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          player_profile_playerByOrg_delete({
            flowId: row.flowId
          }).then(res => {
            if (res.code === 200) {
              this.defaultDataList.forEach((item,index)=>{
              	if(item.flowId == row.flowId){
              	  this.defaultDataList.splice(index,1)
              	}
              })
              this.init_data(this.defaultDataList)
              this.findList()
              this.$message.success('删除成功！')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      checkedEvent(row) {
        this.$confirm('注意：审核过之后，该条数据将不能再被修改', '审核', {
          confirmButtonText: '√通过审核',
          cancelButtonText: '放弃审核',
          type: 'warning'
        }).then(() => {
          player_profile_check({
            flowId: row.flowId
          }).then(res => {
            if (res.code === 200) {
              this.player_profile_playerByOrg()
              this.$message.success('已审核.！')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消审核.'
          });
        });
      },
      enrollDivIPRIdChange() {
          this.insertForm.enrollDivCTId = ''
          this.insertForm.enrollDivId = ''
          if(this.insertForm.enrollDivIPRId){
            changeDivisionCY({enrollDivIPRId:this.insertForm.enrollDivIPRId}).then(res=>{
              if(res.code === 200) {
                this.enrollDivCTIdData = res.data
              }
            })
          }else{
            this.enrollDivCTIdData = []
            this.enrollDivIdData = []
          }

        },
      enrollDivCTIdChange() {
        this.insertForm.enrollDivId = ''
        if(this.insertForm.enrollDivCTId){
          changeDivision({enrollDivCTId:this.insertForm.enrollDivCTId}).then(res=>{
            if(res.code === 200) {
              this.enrollDivIdData = res.data
            }
          })
        }else{
          this.enrollDivIdData = []
        }
      },
      nativePlacePRIdChange() {
          this.insertForm.nativePlaceCTId = ''
          this.insertForm.nativePlaceId = ''
          if(this.insertForm.nativePlacePRId){
            changeDivisionCY({enrollDivIPRId:this.insertForm.nativePlacePRId}).then(res=>{
              if(res.code === 200) {
                this.nativePlaceCTIdData = res.data
              }
            })
          }else{
            this.nativePlaceCTIdData = []
            this.nativePlaceIdData = []
          }

        },
      nativePlaceCTIdChange() {
        this.insertForm.nativePlaceId = ''
        if(this.insertForm.nativePlaceCTId){
          changeDivision({enrollDivCTId:this.insertForm.nativePlaceCTId}).then(res=>{
            if(res.code === 200) {
              this.nativePlaceIdData = res.data
            }
          })
        }else{
          this.nativePlaceIdData = []
        }
      },
      editEvent(row,rowIndex) {
        this.currentIndex = rowIndex
        if (row.checkUserId!=0) {
          this.$message.warning('已经审核过的数据不能修改')
        } else {
          this.flowId = row.flowId
          this.insertForm.pId = row.pId+''
          this.insertForm.address = row.address
          this.insertForm.politicalId = row.politicalId!=0?row.politicalId+'':''
          this.insertForm.educationTypeId = row.educationTypeId!=0?row.educationTypeId+'':''
          this.insertForm.enrollId = row.enrollId
          this.insertForm.enrollDivIPRId = row.enrollDivIPRId!=0?row.enrollDivIPRId+'':''
          this.enrollDivIPRIdChange()
          this.insertForm.enrollDivCTId = row.enrollDivCTId!=0?row.enrollDivCTId:''
          this.enrollDivCTIdChange()
          this.insertForm.enrollDivId = row.enrollDivId!=0?row.enrollDivId:''
          this.insertForm.nativePlacePRId = row.nativePlacePRId!=0?row.nativePlacePRId+'':''
          this.nativePlacePRIdChange()
          this.insertForm.nativePlaceCTId = row.nativePlaceCTId!=0?row.nativePlaceCTId:''
          this.nativePlaceCTIdChange()
          this.insertForm.nativePlaceId = row.nativePlaceId!=0?row.nativePlaceId:''
          this.insertForm.phone = row.phone
          this.insertForm.email = row.email
          this.isInsert = false
          this.insertModal = true
        }
      },
      insertSave() {
        this.$refs.insertForm.validate((valid) => {
          if (valid && this.openDateValid) {
            let data = {}
            if (this.insertForm.pId) data.pId = this.insertForm.pId
            if (this.insertForm.address) data.address = this.insertForm.address
            if (this.insertForm.politicalId) data.politicalId = this.insertForm.politicalId
            if (this.insertForm.educationTypeId) data.educationTypeId = this.insertForm.educationTypeId
            if (this.insertForm.enrollId) data.enrollId = this.insertForm.enrollId
            if (this.insertForm.enrollDivIPRId) data.enrollDivIPRId = this.insertForm.enrollDivIPRId
            if (this.insertForm.enrollDivCTId) data.enrollDivCTId = this.insertForm.enrollDivCTId
            if (this.insertForm.enrollDivId) data.enrollDivId = this.insertForm.enrollDivId
            if (this.insertForm.nativePlacePRId) data.nativePlacePRId = this.insertForm.nativePlacePRId
            if (this.insertForm.nativePlaceCTId) data.nativePlaceCTId = this.insertForm.nativePlaceCTId
            if (this.insertForm.nativePlaceId) data.nativePlaceId = this.insertForm.nativePlaceId
            if (this.insertForm.email) data.email = this.insertForm.email
            if (this.insertForm.phone) data.phone = this.insertForm.phone
            if (this.isInsert) {
              player_profile_insert(data).then(res => {
                if (res.code === 200) {
                  this.dataList.unshift(res.data)
                  this.defaultDataList.push(res.data)
                  this.init_data(this.defaultDataList)
                  this.page.totalResult+=1
                  this.$refs.XTable.setCurrentRow(this.dataList[0])
                  this.$message.success('保存成功')
                  this.insertSuccess = true
                  this.insertModal = false
                } else {
                  this.$message.error(res.message)
                }
              })
            } else {
              data.flowId = this.flowId
              player_profile_update(data).then(res => {
                if (res.code === 200) {
                  Object.assign(this.dataList[this.currentIndex],res.data)
                  this.defaultDataList.forEach(item=>{
                  	if(res.data.flowId==item.flowId){
                  	  Object.assign(item,res.data)
                  	}
                  })
                  this.init_data(this.defaultDataList)
                  this.$message.success('修改成功')
                  this.insertModal = false
                } else {
                  this.$message.error(res.message)
                }
              })
            }
          } else {
            return false;
          }
        })
      },
      insertEvent() {
        this.insertForm = {
          playerId: '',
          address: '',
          politicalId: '',
          educationTypeId: '',
          enrollId: '',
          enrollDivIPRId: '',
          enrollDivCTId: '',
          enrollDivId: '',
          nativePlacePRId: '',
          nativePlaceCTId: '',
          nativePlaceId: '',
          phone: '',
          email: '',
        }
        this.sportChangesportItemData = {}
        this.isInsert = true
        this.insertModal = true
      },
      exportData() {
        this.$refs.XTable.exportData({
          filename: this.$store.state.user.org+'运动员'+this.$route.meta.title, type: 'csv',
          data: this.search?this.dataList:this.defaultDataList
        })
      },
      printEvent() {
        let topHtml = '<h2>基本信息</h2>'
        this.searchEvent()
        setTimeout(() => {
          this.$refs.XTable.print({
            sheetName: '基本信息',
            beforePrintMethod: ({
              content
            }) => {
              return topHtml + content
            }
          })
        }, 1000)
      },
      searchEvent() {
        const filterName = this.$XEUtils.toValueString(this.filterName).trim().toLowerCase()
        if (filterName) {
          const filterRE = filterName
          const searchProps = this.searchProps
          const rest = this.defaultDataList.filter(item => searchProps.some(key => this.$XEUtils.toValueString(item[
            key]).toLowerCase().indexOf(filterName) > -1))
          this.dataList = rest.map(row => {
            const item = Object.assign({}, row)
            searchProps.forEach(key => {
              item[key] = this.$XEUtils.toValueString(item[key]).replace(filterRE, match => match)
            })
            return item
          })
          this.page = {
          	currentPage: 1,
          	pageSize: this.dataList.length,
          	totalResult: this.dataList.length
          }
          this.search = true
        } else {
          this.dataList = this.defaultDataList
     }
      },
      player_profile_playerByOrg() {
        this.loading = true
        player_profile_playerByOrg().then(res => {
          if (res.code === 200 && res.data.length != 0) {
            this.init_data(res.data)
          } else {
            this.defaultDataList = []
          }
        }).then(() => {
          this.findList()
        })
      },
      init_data(data) {
        if(data.length==0) return
        data.forEach(item => {
          item.player = this.dict_player[item.pId]
          item.birthday = this.$moment(this.dict_pid_birthday[item.pId]).format('YYYY-MM-DD') == 'Invalid date' ?
            '' : this.$moment(this.dict_pid_birthday[item.pId]).format('YYYY-MM-DD')
          item.gender = this.dict_pid_gender[item.pId]
          if (this.dict_pid_gender[item.pId] == '男') {
            item.genderId = '1'
          } else {
            item.genderId = '2'
          }
          let coachArr = this.dict_pid2coach[item.pId].split(',')
          let arr = []
          coachArr.forEach(coach=>{
            arr.push(this.dict_coach[coach])
          })
          item.coach = arr.join(',')
          item.idNo = this.dict_pid_idNo[item.pId]
          item.political = this.dict_political_type[item.politicalId]
          item.sport = this.dict_pid_sport[item.pId]
          item.educationType = this.dict_education_type[item.educationTypeId]
          item.enrollDivIPR = this.dict_divisionIdPR[item.enrollDivIPRId]
          item.enrollDivCT = this.dict_divisionIdCY[item.enrollDivCTId]
          item.enrollDiv = this.dict_division[item.enrollDivId]
          item.nativePlacePR = this.dict_divisionIdPR[item.nativePlacePRId]
          item.nativePlaceCT = this.dict_divisionIdCY[item.nativePlaceCTId]
          item.nativePlace = this.dict_division[item.nativePlaceId]
          item.validDateBegin = this.$moment(item.validDateBegin).format('YYYY-MM-DD') == 'Invalid date' ? '' : this
            .$moment(item.validDateBegin).format('YYYY-MM-DD')
          item.validDateEnd = this.$moment(item.validDateEnd).format('YYYY-MM-DD') == 'Invalid date' ? '' : this
            .$moment(item.validDateEnd).format('YYYY-MM-DD')
          item.createUser = this.dict_userId2empName[item.createUserId]
          if (item.checkUserId&&item.checkUserId!='0') {
            item.checkUser = this.dict_userId2empName[item.checkUserId]
          } else {
            item.checkUser = '未审核'
          }
        })
        this.searchProps = Object.keys(data[0])
        this.defaultDataList = data
      },
      init_dict() {
        return new Promise((resolve, reject) => {
          getPersonByOrgPlayer().then(res => {
            let dict_player = {}
            let dict_pid_birthday = {}
            let dict_pid_gender = {}
            let dict_pid_idNo = {}
            res.data.forEach(item => {
              dict_player[item.pid] = item.name
              dict_pid_birthday[item.pid] = item.birthday
              dict_pid_gender[item.pid] = this.dict_gender[item.genderId]
              dict_pid_idNo[item.pid] = item.idNo
            })
            this.dict_player = dict_player
            this.dict_pid_birthday = dict_pid_birthday
            this.dict_pid_gender = dict_pid_gender
            this.dict_pid_idNo = dict_pid_idNo
          }).then(() => {
            pId2sportIds4player().then(res => {
              this.dict_pid_sport = res
            }).then(() => {
              sport_by_org().then(res2 => {
                this.dict_sport = res2
                for (let key in this.dict_pid_sport) {
                  let arr = this.dict_pid_sport[key].split(',')
                  let sportArr = []
                  arr.forEach(item => {
                    sportArr.push(this.dict_sport[item])
                  })
                  this.dict_pid_sport[key] = sportArr.join(',')
                }
                resolve()
              })
            })
          })
          pId2coachIds().then(res=>{
            this.dict_pid2coach = res
          })
          availableCoach().then(res=>{
            this.dict_coach = res
          })
          userId2EmpName().then(res => {
            this.dict_userId2empName = res
          })
          political_type().then(res=>{
            this.dict_political_type = res
          })
          selectDivisionPR().then(res=>{
            this.dict_divisionIdPR = res
          })
          selectDivisionCY().then(res=>{
            this.dict_divisionIdCY = res
          })
          selectDivisionCO().then(res=>{
            this.dict_division = res
          })
        })
      },
      handlePageChange({
        currentPage,
        pageSize
      }) {
        this.page.currentPage = currentPage
        this.page.pageSize = pageSize
        this.findList()
      },
      sortChange() {
      	if(!this.search){
          this.page.currentPage = 1
      	  this.page.pageSize = this.defaultDataList.length
      	  this.findList()
      	}else{
      	  this.page.pageSize = this.dataList.length
      	}
      },
      findList() {
        this.loading = true
        setTimeout(() => {
          if(this.insertSuccess){
            this.page.totalResult = this.defaultDataList.length
            this.page.currentPage = Math.ceil(this.page.totalResult/this.page.pageSize)
            this.dataList = this.defaultDataList.slice((this.page.currentPage - 1) * this.page
              .pageSize, this.page.currentPage * this.page.pageSize)
            this.$refs.XTable.setCurrentRow(this.dataList[this.dataList.length-1])
            this.insertSuccess = false
          }else{
            this.page.totalResult = this.defaultDataList.length
            this.dataList = this.defaultDataList.slice((this.page.currentPage - 1) * this.page
              .pageSize, this.page.currentPage * this.page.pageSize)
          }
          this.loading = false
        }, 300)
      },
    }
  }
</script>

<style lang="scss" scoped>

</style>
